package com.cph.project.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.cph.aop.BusinessType;
import com.cph.aop.Log;
import com.cph.redis.RedisCache;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cph.project.pojo.SysUser;
import com.cph.project.service.ISysUserService;
import com.cph.common.http.HttpResponseResult;
import com.cph.common.http.HttpStatus;
import com.cph.framework.page.TableDataInfo;

/**
 * 用户信息Controller
 *
 * @author cph
 * @date 2020-02-26
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/oa/user")
@Slf4j
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询用户信息列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询用户信息列表", notes = "/oa/user/list")
   @ApiImplicitParam(name = "sysUser", value = "用户信息", dataType = "SysUser")
    @Log(title = "用户管理", businessType = BusinessType.OTHER)
    public TableDataInfo list(SysUser sysUser) {
        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
        for (SysUser user : list
        ) {
            redisCache.setCacheObject(user.getNickName(), user);
        }
        SysUser redisUser = redisCache.getCacheObject("常鹏辉");
        log.info(JSON.toJSONString(redisUser));
        return getDataTable(list);
    }
//
//    /**



//     * 导出用户信息列表
//     */
//    @GetMapping("/export")
//    public AjaxResult export(SysUser sysUser)
//    {
//        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
//        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
//        return util.exportExcel(list, "user");
//    }

    /**
     * 获取用户信息详细信息
     */
    @GetMapping(value = "/{userId}")
    public HttpResponseResult getInfo(@PathVariable("userId") Long userId) {
        return HttpResponseResult.success(sysUserService.selectSysUserById(userId));
    }

    /**
     * 新增用户信息
     */
    @PostMapping
    public HttpResponseResult add(@RequestBody SysUser sysUser) {

        return toHttpResponseResult(sysUserService.insertSysUser(sysUser));
    }

    /**
     * 修改用户信息
     */
    @PutMapping
    public HttpResponseResult edit(@RequestBody SysUser sysUser) {
        return toHttpResponseResult(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除用户信息
     */
    @DeleteMapping("/{userIds}")
    public HttpResponseResult remove(@PathVariable Long[] userIds) {
        return toHttpResponseResult(sysUserService.deleteSysUserByIds(userIds));
    }

//    /**
//     * 设置请求分页数据
//     */
//    protected void startPage() {
//        PageDomain pageDomain = TableSupport.buildPageRequest();
//        Integer pageNum = pageDomain.getPageNum();
//        Integer pageSize = pageDomain.getPageSize();
//        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
//            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
//            PageHelper.startPage(pageNum, pageSize, orderBy);
//        }
//    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected HttpResponseResult toHttpResponseResult(int rows) {
        return rows > 0 ? HttpResponseResult.success() : HttpResponseResult.error();
    }
}
